/**
 * 基本药物弹窗模块，点击图表首页后初始化弹窗数据
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","valueUtil","panelUtil","btnServ","datagridServ"],
		function($,sysmanMain,valueUtil,panelUtil,btnServ,datagridServ) {

		var rootid = '';
		
		var window_id = 'raise-charge-pop-win';
		
		var module_code ='raiseChargeInfo';
		var module_code_head = 'raiseCharge';
		
		var queryParam ;
		
		var _buttonConfig;
		var public_method ={

			openPage:function(_options){
				sysmanMain.setModuConf(module_code,{initQueryParams:{condition:_options, orderBy:'number', groupBy:'drug'}})
				var config = {
					href:'/'+sysmanMain.getRootPath()+'/' + module_code_head + '/query/popPage.action',
					window:$('#' + window_id),
					width:'900',
					height:'700',
					flag:false,
					onClose:private_method.closePage
				}
				config.window = $('#' + window_id)
				queryParam = _options;
				var _param = $.extend({},config,_options)
				datagridServ.windowEdit(_param);
			},
			rowClick:function(index,data){
				
				if(window.console)
					console.info('inpatient rowClick event fired ...')
				var _config = sysmanMain.getModuConf('inpatientItemFee');
				if(_config ==null){
					_config = {}					
				}	
				
				_config.initQueryParams = {field:'patient_id',fieldVal:data.patient_id}
				sysmanMain.setModuConf('inpatientItemFee',_config)
				sysmanMain.medicalInpatientItemFeeSelect()
			},
			clickBizRow:function(index,row){
				var target, aim
				var a1 = $('#' + rootid + ' div.' + 'list-panel')
				var a2 = $('#' + rootid + ' div.' + 'dept-panel')
				var a3 = $('#' + rootid + ' div.' + 'doct-panel')
				if(panelUtil.getPanelState({target:a1}) == 1){
					aim = "dept-panel"
					private_method.changePanel({aimPanel:aim})
					var _param =$.extend({},queryParam,{dept_code:row.dept_code,data_type:'3'})
					private_method.initDetail(_param)
				}
				else if(panelUtil.getPanelState({target:a2}) == 1){
					aim = "doct-panel"
					private_method.changePanel({aimPanel:aim})
					var _param =$.extend({},{dept_code:row.dept_code,monitor_year_month:queryParam.monitor_year_month,doct_code:row.doct_code})
					private_method.initPatient(_param)
				}
				if(window.console){
					console.info('biz row clicked...')
				}
			},
			/**
			 * 初始化新增页面
			 * @param _options.record 列表选中行
			 * @param _options.form 页面form对象
			 * @param _options.rootId 页面根节点
			 */
			initMain:function(_options){
				var _rootpath = '/'+sysmanMain.getRootPath()
				rootid = _options.rootId
				var t = sysmanMain.getCurrentModuConfig()
				_buttonConfig = {
					'返回':private_method.backWay
				}
				var _deptEl = $('#'+window_id +' #indicator_unit_name')
				if(_deptEl.length==1){
					_deptEl.textbox({
						onClickButton:function(){
							deptSelect.openPage({
								deptid:window_id +' #indicator_unit_id',
								deptname:window_id + ' #indicator_unit_name'
							})
						}
					})
				}
				private_method.bindBtnEvent()
			},
			buttonExtends:function(){
				
				return _buttonConfig
			}
			
		}
		
		var private_method ={
			backWay:function(){
				var _t =  $('#'+window_id + ' div.details-panel table.dg-list')
				_t.empty();
				private_method.changePanel({aimPanel:"list-panel"})
			},
			initDetail:function(_options){
				//定位明细列表
				var _t =  $('#'+window_id + ' div.details-panel table.dg-list')
				//清空明细列表
				console.log(_t.datagrid('options').url);
				if(!_t.datagrid('options') || !_t.datagrid('options').url){
					//根据条件加载明细列表
					_t.datagrid({
						url:'/'+sysmanMain.getRootPath()+'/' + module_code_head + 'query/getListDomain.action',
						queryParam:{condition:_options}
					})
				}else{
					_t.datagrid('load',{condition:_options})
				}
			},
			initPatient:function(_options){
				//定位明细列表
				var _t =  $('#'+window_id + ' div.patient-panel table.dg-list')
				//清空明细列表
				//console.log(_options.url);
				_t.datagrid({url:'/'+sysmanMain.getRootPath()+'/' + 'baseOutpatient' + '/query/getListPatientDomain.action',
				queryParams:{condition:_options}})
			},
			/**
			 * 
			 */
			changePanel:function(_options){
				var tp
				if(_options.aimPanel){
					tp = $('#' + rootid + ' div.easyui-panel')
					if (tp.length > 0) {
						tp.panel('close')
						console.info("here all closed")
					}
					tp = $('#' + rootid + ' div.' + _options.aimPanel)
					tp.panel('open')
				}
				else{
					var $listPanel = $('#' +window_id +'  div.list-panel' )
					var $detailsPanel = $('#' +window_id +' div.details-panel' )
					
					panelUtil.togglePanel({target:$listPanel})
					panelUtil.togglePanel({target:$detailsPanel})
				}
			},
			initEditList:function(){
				
			},
			closePage:function(){
				$('#'+window_id).empty();
			},
			bindBtnEvent:function(){

				var window_btn = $('#'+rootid +' a.function-btn')
				$(window_btn).unbind('click')
				$(window_btn).bind('click',	private_method.function_btn_listenner)

			},
			detailInfo:function(patient_id){
				var _config={}
				_config.initQueryParams = {condition:{outpatient_number:patient_id}}
				sysmanMain.setModuConf('outpatientItemFee',_config)
				sysmanMain.medicalOutpatientItemFeeSelect()
			},

			// 功能按钮监听函数
			function_btn_listenner : function(e) {
				if(window.console){
					console.log('got a return here~~~');
				}
				btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

			}
		}
			
		
		return public_method;
			
})